<?php

class LayoutsController extends Controller
{
	public function actionGet() { 
		$id = Yii::app()->request->getParam('pid',null);
		$project = Layouts::model()->findByAttributes(array(
			'object_id'=>outBin($id)
		));
		if(!empty($project)){
			$data = @CJSON::decode($project->context);
			jsonOut(array(
				'error'=>false,
				'data'=>$data
			));
		}else{
			jsonOut(array(
				'error'=>true,
				'data'=>array()
			));
		}
	}
	public function actionSave() { 
		if(Yii::app()->user->isGuest){
			jsonOut(array(
				'error'=>true,
				'message'=>'Pls login'
			));
		}
		if(empty($_POST)){
			jsonOut(array(
				'error'=>true,
				'message'=>'Form params not found'
			));
		}
		$id = Yii::app()->request->getParam('pid',null);
		$project = Projects::model()->findByPk(outBin($id));
		if(empty($project)){
			jsonOut(array('error'=>true,'message'=>'Project not found'));
		}
		$data = array();
		foreach($_POST as $key=>$value){
			$data[] = $value;
		}
		$layout = Layouts::model()->findByAttributes(array('object_id'=>$project->id));
		if(!empty($layout)) $layout->delete();
		
		$model = new Layouts;
		$model->object_id = $project->id;
		$model->created = time();
		$model->status = 0;
		$model->context = @CJSON::encode($data);
		
		try{
			$model->save();
			jsonOut(array( 'error'=>false,'message'=>'The layouts has been successfully' ));
		}catch(Exception $e){
			jsonOut(array('error'=>true,'message'=>$e->getMessage()));
		}
		
	} 
	
	
}